package com.hghivln.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.blog.common.exception.BlogException;
import com.hghivln.blog.entity.SysUser;
import com.hghivln.blog.mapper.SysUserMapper;
import com.hghivln.blog.pojo.dto.UpdatePwdDto;
import com.hghivln.blog.service.SysUserService;
import com.hghivln.blog.shiro.UserRealm;
import com.hghivln.blog.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-09-01
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Override
    public SysUser getByUsername(String username) {
        return getBaseMapper().getByUsername(username);
    }

    @Override
    public void updatePwd(UpdatePwdDto updatePwdDto) {
        SysUser sysUser = getBaseMapper().selectById(updatePwdDto.getId());
        if (!sysUser.getPassword().equals(updatePwdDto.getCurrentPwd())){
            throw new BlogException("当前的密码不正确");
        }
        getBaseMapper().updatePwd(updatePwdDto);
    }

    @Override
    public boolean updateById(SysUser entity) {
        try {
            getBaseMapper().updateById(entity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
